/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.unibo.as.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;



/**
 *
 * @author Johnny
 */
public class ChatPanel extends Composite {
    VerticalPanel vp ;
    private GWTChatServiceAsync chatService;
    private HTML messages;
    private ScrollPanel scrollPanel;

    TextArea chatText;

    public ChatPanel() {

        chatService = GWT.create(GWTChatService.class);
        vp = new VerticalPanel();
        vp.setSize("787px", "188px");
        

        messages = new HTML();
        scrollPanel = new ScrollPanel();
	scrollPanel.setHeight("250px");
	scrollPanel.add(messages);
        scrollPanel.setSize("781px", "140px");

        vp.add(scrollPanel);


        vp.setCellHeight(scrollPanel, "140px");
        vp.setCellWidth(scrollPanel, "781px");
        

        final TextBox inputBox = new TextBox();
        vp.add(inputBox);
        inputBox.setWidth("779px");



        final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean result) {
                    if(result){
                        inputBox.setText("");
                    }
                    else{
                    chatAppend("----CONNECTION ERROR USER HAVE NO SESSION----");

                    }

                }

                @Override
                public void onFailure(Throwable caught) {
                    chatAppend("----CONNECTION ERROR----");
                }
         };

        inputBox.addKeyPressHandler(new KeyPressHandler(){
            @Override
            public void onKeyPress(KeyPressEvent event) {
                if(event.getCharCode()== 13){
                    chatService.sendMessage(inputBox.getText(), callback);

                }
            }

        });

        initWidget(vp);




    }

    public void chatAppend(String line){
        
        DivElement div = Document.get().createDivElement();
	div.setInnerText(line);
	messages.getElement().appendChild(div);
	scrollPanel.scrollToBottom();

         
       
    }

    public static GWTChatServiceAsync getChatService() {
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of the interface. The cast is always safe because the
        // generated proxy implements the asynchronous interface automatically.

        return GWT.create(GWTChatService.class);
    }
    
}
